package com.account.boot.service.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.account.boot.dao.UserMapper;
import com.account.boot.model.User;
import com.account.boot.model.vo.UserVo;

@Service
public class UserMapperService {

	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 名称获取用户
	 * @param name
	 * @return
	 */
	public UserVo findByName(String name) {
		User user = userMapper.selectByName(name);
		if(user != null) {
			return new UserVo(user);
		}
		return null;
	}
	
	/**
	 * 分页获取用户
	 * @param index
	 * @return
	 */
	public List<UserVo> findByPage(Integer index) {
		if(index == null || index < 0) {
			index = 0;
		}
		List<UserVo> userVos = new ArrayList<UserVo>();
		List<User> users = userMapper.selectByPage(index);
		for(int i = 0; i < users.size(); i++) {
			userVos.add(new UserVo(users.get(i)));
		}
		return userVos;
	}
	
	/**
	 * 通过uid获取用户对象
	 * @param uid
	 * @return
	 */
	public UserVo findByUid(String uid) {
		if(StringUtils.isEmpty(uid)) {
			return null;
		}
		User user = userMapper.selectByUid(uid);
		if(user == null)
		{
			return null;
		}
		UserVo userVo = new UserVo(user);
		return userVo;
	}
}
